package com.platform.common.utils.file.minio;

import com.alibaba.fastjson2.JSONObject;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.file.FileUtils;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author Huangkai
 */
@Slf4j
@Component
public class MinioFileUtils {

    @Resource
    CosClientConfigMap cosClientConfigMap;

    public String fileUpload(String localFilePath, String fileType) {
        File file = new File(localFilePath);
        if(!file.exists()){
            throw new BusinessException(ResultEnum.FILE_NOT_EXIST);
        }
        Integer type = -1;
        //如果存在文件类型的
        if(FileUtils.fileTypeMap.containsKey(fileType)){
            type = FileUtils.fileTypeMap.get(fileType);
        }
        String key = FileKeyUtils.getFileKey(file, type);
        try{
            upload(localFilePath, key);
        }catch (Exception e){
            throw new BusinessException(ResultEnum.UPLOAD_FAIL);
        }
        file.delete();
        return key;
    }


    public void upload(String localFilePath, String key) {
        // 构造一个带指定 Region 对象的配置类，这里以华东地区为例
        Configuration cfg = new Configuration(Region.region2());
        // 创建上传管理器
        UploadManager uploadManager = new UploadManager(cfg);
        // 生成鉴权对象
        Auth auth = Auth.create(cosClientConfigMap.getConfig().getAccessKey(), cosClientConfigMap.getConfig().getSecretKey());
        log.info("参数{}", cosClientConfigMap);
        // 生成上传凭证
        String upToken = auth.uploadToken(cosClientConfigMap.getConfig().getBucketName());
        log.info("token{}", upToken);
        try {
            // 调用上传方法
            Response response = uploadManager.put(localFilePath, key, upToken);
            // 解析上传结果
            if (response.isOK()) {
                log.info("上传成功");
            }else{
                log.info("上传失败");
            }
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[]args){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
        UploadManager uploadManager = new UploadManager(cfg);
        String accessKey = "45_p6V3jYlFpWD_7EE-VA13mP1mB-nFj9xLX7xEW";
        String secretKey = "fjXX582Nn7QO8l3_DxHFdO7dOzD9n24eacbUs_E0";
        String bucket = "xingyun-yuliang";
        String localFilePath = "C:\\Users\\huangkai\\Desktop\\系统平台开发需求.pdf";
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            log.info("结果{}", JSONObject.toJSONString(response));
        } catch (QiniuException ex) {
            ex.printStackTrace();
            if (ex.response != null) {
                System.err.println(ex.response);
                try {
                    String body = ex.response.toString();
                    System.err.println(body);
                } catch (Exception ignored) {
                }
            }
        }
    }
}
